<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	SmitSession.php - Session Handling Class
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class SmitSession
{
	public $base;
	
	private static $instance;
	
	public $rows = FALSE;

	public $data = array();

	public $user = array();
	
	public $session_checked = FALSE;

	private function __construct()
	{
		$this->base = SmitBase::getInstance();
		if(!$this->session_exists())
		{
			$this->create_guest_session();
		}
		else
		{
			$this->update_action();
			if($this->logged_in())
			{
				$this->base->db->select('*')->from('users')->where('id', $this->data['user_id'])->run();
				$this->user = $this->base->db->row();
				//$this->base->debug->notice(nl2br(print_r($this->user, 1)));
			}
		}
	}

    public function __clone()
    {
        trigger_error('cloning denied.', E_USER_ERROR);
    }

	static function getInstance()
	{
		if(!is_object(self::$instance))
		{
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function session_exists()
	{
		$cookie = $this->base->lib->input_cookie('session_public');
		if($cookie === FALSE)
		{
			return FALSE;
		}

		// Run the query only if no rows have been fetched
		if($this->rows === FALSE)
		{
			$this->base->db->select('*')->from('sessions')->where('sid', $cookie)->run();
			$this->rows = $this->base->db->num_rows();
			if($this->rows > 0)
			{
				$this->data = $this->base->db->row();
				if($this->base->lib->input_server('HTTP_USER_AGENT', 255) == $this->data['useragent'])
				{
					return TRUE;
				}
			}
		}

		// So that multiple calls to this function work
		if($this->rows > 0)
		{
			return TRUE;
		}
		return FALSE;
	}

	public function logged_in()
	{
		if($this->session_exists())
		{
			if($this->data['user_id'] > 0)
			{
				return TRUE;
			}
		}
		return FALSE;
	}

	public function fetch_session()
	{
		return $this->base->lib->input_cookie('session_public');
	}

	public function create_guest_session()
	{
		$ipaddress = $this->base->lib->input_server('REMOTE_ADDR', 255);
		$useragent = $this->base->lib->input_server('HTTP_USER_AGENT', 255);
		$session = md5($ipaddress . $useragent . microtime(true) . rand(1, 500) . $this->base->vars['root']['private_key']);
		$this->base->db->insert('sessions', array(
		'sid' => $session,
		'user_id' => 0,
		'address' => $ipaddress,
		'useragent' => $useragent,
		'last_action' => $this->base->lib->timestamp(),
		));
		$this->base->lib->setcookie('session_public', $session, $this->base->lib->timestamp() + (60*60*24*100));
	}

	public function create_member_session($user_id)
	{
		$ipaddress = $this->base->lib->input_server('REMOTE_ADDR', 255);
		$useragent = $this->base->lib->input_server('HTTP_USER_AGENT', 255);
		$session = md5($ipaddress . $useragent . microtime(true) . rand(1, 500) . $this->base->vars['root']['private_key']);
		$this->base->db->insert('sessions', array(
		'sid' => $session,
		'user_id' => $user_id,
		'address' => $ipaddress,
		'useragent' => $useragent,
		'last_action' => $this->base->lib->timestamp(),
		));
		$this->base->lib->setcookie('session_public', $session, $this->base->lib->timestamp() + (60*60*24*100));
	}

	public function update_action()
	{
		if($this->session_exists())
		{
			$session = $this->fetch_session();
			$this->base->db->update('sessions', array('last_action' => $this->base->lib->timestamp()))->where('sid', $session)->run();
		}
	}

	public function destroy_session()
	{
		if($this->session_exists())
		{
			$session = $this->fetch_session();
			$this->base->db->delete()->from('sessions')->where('sid', $session)->run();
			$this->base->lib->setcookie('session_public', NULL, $this->base->lib->timestamp() - (60*60*24*100));
		}
	}
}

?>